import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex); //把vuex注入到所有的子组件
import * as api from "../api";
import allRoutes from "@/router/allRoutes";
import dynamicRoutes from "@/router/dynamicRoutes";
import recursionRoutes from "@/untils/recursionRoutes";
import router from "../router";

let userInfo = localStorage.getItem("qf-userInfo") || "{}";
export default new Vuex.Store({
  //strict:true,//只是警告一下不要在mutation以外的处理函数中修改state
  //vuex的五大核心
  state: {
    userInfo: JSON.parse(userInfo),
    // 侧边栏菜单
    sideMenu: [],
    // 面包屑
    breadCrumb: [],
    tableData: [],
    dialogVisible: false,
    dialogTitle: "增加学员信息",
    loading: true,
    currentRow: {},
    stuTotal: 0,
    stuPageSize: 10,
    orderData: [],
  },
  mutations: {
    SHOW_LOADING(state) {
      state.loading = true;
    },
    HIDE_LOADING(state) {
      state.loading = false;
    },
    SET_STU_DATA(state, payload) {
      state.tableData = payload.data;
      state.loading = false;
      state.stuTotal = payload.total;
    },
    SHOW_DIALOG(state) {
      state.dialogVisible = true;
    },
    HIDE_DIALOG(state) {
      state.dialogVisible = false;
    },
    CHANGE_DIALOG_TITLE(state, payload) {
      state.dialogTitle = payload;
    },

    //保存用户信息
    SET_USER_INFO(state, payload) {
      state.userInfo = payload;
    },
    // 设置菜单
    SET_MENULIST(state, payload) {
      // state.sideMenu = payload;
      let target = dynamicRoutes.find((item) => item.path === "/");

      target.children = [...payload, ...target.children];
      router.addRoutes(dynamicRoutes);
      // 预支付订单页面不需要展现出来
      target.children = target.children.filter(
        (item) => item.path !== "pre_order"
      );
      state.sideMenu = target.children;
    },
    SET_BREADCRUMB(state, payload) {
      state.breadCrumb = payload;
    },

    SET_CURRENT_ROW(state, payload) {
      state.currentRow = { ...payload };
    },

    SET_ORDER_DATA(state, payload) {
      state.orderData = payload;
    },
  }, //修改状态的方法
  getters: {},
  actions: {
    // //获取评论列表
    // GET_COMMENT_LIST({ commit }) {
    //   commentApi.getComments().then((res) => {
    //     // console.log(res);
    //     //提交Mutation 变更commentList
    //     commit("SET_COMMENT_LIST", res.data);
    //   });
    // },
    //获取用户菜单
    async GET_MENULIST({ commit }) {
      console.log("action执行了");
      //用于获取服务器中当前用户的菜单
      let res = await api.getMenulistApi();
      let { menuList } = res.data;
      //使用recursionRoutes方法,计算出用户的路由,提交给mutation
      let userRoutes = recursionRoutes(menuList, allRoutes);
      commit("SET_MENULIST", userRoutes);
    },

    GET_STULIST({ commit }, payload = {}) {
      commit("SHOW_LOADING"); //打开加载动画
      let { page = 1, count = 15, classes = "" } = payload; //解构传递的参数
      api.getStuListApi(page, count, classes).then((res) => {
        commit("SET_STU_DATA", res.data);
      });
    },

    //获取订单数据
    GET_ORDER_DATA({ commit }, payload) {
      let order_id = payload || "";
      api.getOrderApi(order_id).then((res) => {
        console.log(res.data);
        if (res.data.state) {
          console.log(res.data.data);
          commit("SET_ORDER_DATA", res.data.data);
        } else {
          this.$message.error("获取订单失败");
        }
      });
    },
  },
  modules: {}, //模块化
});
